package y2015.date0915am.客户端;

import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClient {
    @Test
    public void main() throws Exception {
        Socket socket = null;
        String host = "127.0.0.1";
        int port = 9527;
        // 连接服务器
        try {
            socket = new Socket(host, port);
            System.out.println("成功连接服务器！");
            // 接收欢迎语
            InputStream in =
                    socket.getInputStream();//从此套接字读取字节的输入流。
            BufferedReader br =
                    new BufferedReader(new InputStreamReader(in));
            String line = br.readLine();
            System.out.println("【服务端】" + line);
            // 结束
            br.close();

        } catch (UnknownHostException e) {
            System.out.println("连接服务器失败！");
//			e.printStackTrace();

        } catch (IOException e) {
            System.out.println("连接服务器失败！");
            e.printStackTrace();

        }

    }
}

